package com.imooc.newretail.bpl.ctrl;

import com.imooc.newretail.common.BaseController;
import com.imooc.newretail.common.utils.BeanUtils;
import com.imooc.newretail.common.vo.RV;
import com.imooc.newretail.common.vo.ResultVO;
import com.imooc.newretail.bpl.dto.MenuDTO;
import com.imooc.newretail.bpl.entity.Menu;
import com.imooc.newretail.bpl.service.IMenuService;
import com.imooc.newretail.bpl.dto.MenuTreeDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @program:new-retail-teach
 * @description:菜单管理控制器
 * @author:Hank.He
 * @create:2020-06-23 14:41
 **/
@RestController
@Api(tags = "菜单管理")
@RequestMapping("/sys/menu")
public class MenuController extends BaseController<IMenuService> {

    @Autowired
    IMenuService menuService;

    @ApiOperation(value = "添加和修改菜单对象",notes = "添加和修改菜单对象")
    @PostMapping(value = "/")
    public ResultVO save(@RequestBody MenuDTO menuDTO) {
        Menu menu=BeanUtils.copyProperties(menuDTO,Menu.class);
        menuService.saveOrUpdate(menu);
        return RV.success(menuDTO);
    }

    @ApiOperation(value = "根据ID更新菜单对象")
    @PutMapping("/")
    public ResultVO update(@RequestBody MenuDTO menuDTO) {
        Menu menu=BeanUtils.copyProperties(menuDTO,Menu.class);
//        menuService.saveOrUpdate(menu);
        return new ResultVO(menuService.saveOrUpdate(menu));
    }

    @ApiOperation(value = "根据ID删除菜单对象")
    @ApiImplicitParam(name = "menuId",value = "菜单ID",required = true,defaultValue = "")
    @DeleteMapping("/{menuId}")
    public ResultVO removeByMenuId(@PathVariable("menuId") String menuId) {
        return new ResultVO();
    }

    @ApiOperation(value = "获取菜单树")
    @GetMapping("/tree")
    public ResultVO<List<MenuTreeDTO>> getMenuTree() {
        return new ResultVO(menuService.getMenuTree(null));
    }

    @ApiOperation(value = "获取菜单列表")
    @GetMapping("/list")
    public ResultVO<List<MenuDTO>> getMenuList() {
        return new ResultVO(menuService.list());
    }
    @ApiOperation(value = "导入菜单对象集合，树形结构")
    @PostMapping("/import")
    public ResultVO importMenu(@RequestBody List<MenuTreeDTO> treeDTOList) {
        int rowNum = menuService.importMenu(treeDTOList);
        return new ResultVO(rowNum);
    }
}
